import { createRouter, createWebHashHistory } from 'vue-router'


const routes = [
  {
    path: '/',
    name: 'MyLogin',
    component: () => import('@/views/MyLogin.vue')
  },
  {
    path: '/homeLayout',
    name: 'homeLayout',
    component: () => import('../views/HomeLayout.vue'),
    redirect: "/welcome",
    children: [
      { path: '/welcome', name: 'welcome', component: () => import('../views/Welcome.vue') },
      // { path: '/users', name: 'users', component: () => import('../views/list/User.vue') },
      // { path: '/rights', name: 'rights', component: () => import('../views/list/Rights.vue') },
      // { path: '/roles', name: 'roles', component: () => import('../views/list/Roles.vue') },
      // { path: '/orders', name: 'orders', component: () => import('../views/list/Orders.vue') },
      // { path: '/categories', name: 'categories', component: () => import('../views/list/Categories.vue') },
      // { path: '/goods', name: 'goods', component: () => import('../views/list/Goods.vue') },
      // { path: '/goods/add', name: 'add', component: () => import('../views/list/Add.vue') },
      // { path: '/params', name: 'params', component: () => import('../views/list/Params.vue') },
      // { path: '/reports', name: 'reports', component: () => import('../views/list/Reports.vue') }
    ]
  },
  {
    path: '/test',
    name: 'Test',
    component: () => import(/* webpackChunkName: "about" */ '../views/Test.vue')
  }
]


const router = createRouter({
  history: createWebHashHistory(),
  routes
})

router.beforeEach((to, from, next) => {
  const token = localStorage.getItem('store')
  // console.log(token)
  const isLogin = (to.path === '/')
  // 如果有token，并且是在登录页，那么跳转到首页
  if (token && isLogin) {
    return next({ name: 'homeLayout' })
  }
  // 如果没有token，并不是在登录页，跳转到登录页
  if (!token && !isLogin) {
    return next({ name: '/' })
  }
  next()
})
export default router
